package com.ruoyi.system.service.impl;

import com.ruoyi.common.enums.ArmingStatusEnum;
import com.ruoyi.common.enums.DeviceTypeEnum;
import com.ruoyi.common.enums.ThoroughfareStatus;
import com.ruoyi.system.entity.OneElectronFenceStatus;
import com.ruoyi.system.mapper.OneElectronFenceStatusMapper;
import com.ruoyi.system.service.OneElectronFenceStatusService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 一号库区电子围栏状态(OneElectronFenceStatus)表服务实现类
 *
 * @author makejava
 * @since 2023-10-25 15:54:41
 */
@Service("oneElectronFenceStatusService")
public class OneElectronFenceStatusServiceImpl implements OneElectronFenceStatusService {
    @Resource
    private OneElectronFenceStatusMapper oneElectronFenceStatusDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public OneElectronFenceStatus queryById(Integer id) {
        return this.oneElectronFenceStatusDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param oneElectronFenceStatus 筛选条件
     * @param pageRequest            分页对象
     * @return 查询结果
     */
    @Override
    public Page<OneElectronFenceStatus> queryByPage(OneElectronFenceStatus oneElectronFenceStatus, PageRequest pageRequest) {
        long total = this.oneElectronFenceStatusDao.count(oneElectronFenceStatus);
        return new PageImpl<>(this.oneElectronFenceStatusDao.queryAllByLimit(oneElectronFenceStatus, pageRequest), pageRequest, total);
    }

    @Override
    public List<OneElectronFenceStatus> queryAll(OneElectronFenceStatus oneElectronFenceStatus) {

        List<OneElectronFenceStatus> oneElectronFenceStatuses = oneElectronFenceStatusDao.queryAll(oneElectronFenceStatus);
        if (!CollectionUtils.isEmpty(oneElectronFenceStatuses)) {
            oneElectronFenceStatuses.forEach(var -> {

                var.setSectorStatusName(ThoroughfareStatus.valueOfCode(var.getSectorStatus()).getDesc());
                var.setDeviceType(DeviceTypeEnum.valueOfCode(var.getDeviceType()).getDesc());
                var.setArmingStatus(ArmingStatusEnum.valueOfCode(var.getArmingStatus()).getDesc());

            });
        }

        return oneElectronFenceStatuses;
    }

    public static void main(String[] args) {
        int number = 7;
        String formattedString = String.format("%08d", number);
        System.out.println(formattedString);
    }

    /**
     * 新增数据
     *
     * @param oneElectronFenceStatus 实例对象
     * @return 实例对象
     */
    @Override
    public OneElectronFenceStatus insert(OneElectronFenceStatus oneElectronFenceStatus) {
        this.oneElectronFenceStatusDao.insert(oneElectronFenceStatus);
        return oneElectronFenceStatus;
    }

    /**
     * 修改数据
     *
     * @param oneElectronFenceStatus 实例对象
     * @return 实例对象
     */
    @Override
    public OneElectronFenceStatus update(OneElectronFenceStatus oneElectronFenceStatus) {
        this.oneElectronFenceStatusDao.update(oneElectronFenceStatus);
        return this.queryById(oneElectronFenceStatus.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.oneElectronFenceStatusDao.deleteById(id) > 0;
    }

    @Override
    public void delete() {
        oneElectronFenceStatusDao.delete();
    }
}
